﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.Api.Maps.Service.StaticMaps;

using System.Threading;

/// NExt Viewer project
/// ID : optlabSoar / GAoptlab3132

namespace NextViewer
{
    public partial class NextViewer : Form
    {
        public NextViewer()
        {
            InitializeComponent();
            this.MouseWheel += new MouseEventHandler(pbMap_MouseWheel);
            this.pbMap.MouseWheel += new MouseEventHandler(pbMap_MouseWheel);
            shell = new Shell(this);

            DBMS.GetDBMS().GetResult("SELECT * FROM db_owner.UserInfo WHERE ID='358414046179405'");
            //D SELECT * FROM db_owner.UserInfo WHERE ID='358414046179405 ORDER BY time'

            Http http = new Http(this);
            StaticMap sm = new StaticMap();
            //sm.Center = "37.35751,126.9285";
            sm.Size = "640x640";
            //sm.Zoom = "16";
            sm.Sensor = "false";
            //sm.Path = "color:0x0000ff|weight:5|37.35809,126.9298|37.329,126.8311";
            //sm.Path = "color:0x0000ff|weight:5|37.45631,126.9498|37.45631 126.9498";

            //Image image = http.UpdateMap(sm);
            //this.pbMap.Image = image;
            //this.pbMap.SizeMode = PictureBoxSizeMode.AutoSize;
        }
        public void updatePic()
        {
            if (pbMap2.Image != null)
                pbMap3.Image = pbMap2.Image;
            if (pbMap.Image != null)
                pbMap2.Image = pbMap.Image;
        }
        public void updatePic(StaticMap sm)
        {
            Http http = new Http(this);
            Image image = http.UpdateMap(sm);
            if (image != null)
            {
                SetText(sm.ToUri().ToString());
                if (pbMap2.Image != null)
                    pbMap3.Image = pbMap2.Image;
                if (pbMap.Image != null)
                    pbMap2.Image = pbMap.Image;
                pbMap.Image = image;
                pbMap.SizeMode = PictureBoxSizeMode.AutoSize;
            }
        }
        public void updateConsole(string[] str)
        {
            foreach (string st in str)
            {
                tbConsole.Text += st + Environment.NewLine;
            }
            tbConsole.Update();
        }
        public void SetText(string str)
        {
            tbConsole.AppendText(str + Environment.NewLine);
            tbConsole.Update();
        }
        public void SetOpacity(double rate)
        {
            this.Opacity = rate;
            this.Update();
        }
        public void ClearConsole()
        {
            tbConsole.Clear();
            tbConsole.Update();
        }

        private Shell shell;

        private void tbShell_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SetText(tbShell.Text);
                shell.Parser(tbShell.Text, this);
                tbShell.Clear();
                index = shell.mCmds.Count;
            }
            else if (e.KeyCode == Keys.Up)
            {
                if (shell.mCmds.Count == 0)
                    return;
                index--;
                if (index < 0)
                    index = 0;
                tbShell.Clear();
                tbShell.AppendText(shell.mCmds[index]);
            }
            else if (e.KeyCode == Keys.Down)
            {
                if (shell.mCmds.Count == 0)
                    return;
                index++;
                if (index > shell.mCmds.Count - 1)
                    index = shell.mCmds.Count - 1;
                tbShell.Clear();
                tbShell.AppendText(shell.mCmds[index]);
            }

        }
        public void DrawPoints(double[] s, double[] t, Nodes forest)
        {

            Bitmap bmp = new Bitmap(Glovar.bmp_hsize, Glovar.bmp_vsize);//pbMap.Image);

            Graphics gp = Graphics.FromImage(bmp);
            int rad = 1;
            
            Pen pen = new Pen(Color.Blue, 4);
            pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
            pen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
            Pen penRed = new Pen(Color.Red, 4);
            penRed.StartCap = System.Drawing.Drawing2D.LineCap.Round;
            penRed.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
            SolidBrush blush = new SolidBrush(Color.Red);

            bool bOrgin=false;
            int ox=0, oy=0;     //floating 대상을 움직이는 
            
            mForest = forest;
            
            double x = 0, y = 0, xp = 0, yp = 0;

            if (scale == null)
                scale = new Double[3];
            if (trans == null)
                trans = new Double[3];
            for (int k = 0; k < 3; k++)
            {
                scale[k] = s[k];
                trans[k] = t[k];
            }

            int i;
            Node nd, nd2;
            forest.StartTravel();
            for (i = 0; i < forest.Count(); i++)
            {
                nd = forest.GetNext();
                //x = (nd.x * 1 - (int)(nd.x * 1)) * 10000;
                //y = (nd.y * 1 - (int)(nd.y * 1)) * 10000;
                x = (nd.x * 1 )* 10000;
                y = (nd.y * 1 ) * 10000;
                x = (s[0] * x + s[0] * t[0]);
                y = (s[1] * y + s[1] * t[1]);
                
                foreach (DictionaryEntry de in nd.edge.edges)
                {

                    Edge ed = (Edge)de.Value;
                    nd2 = forest.GetNode(ed.id);

                    //xp = (nd2.x *1  - (int)(nd2.x *1)) * 10000;
                    //yp = (nd2.y *1 - (int)(nd2.y *1)) * 10000;
                    xp = (nd2.x)  * 10000;
                    yp = (nd2.y) * 10000;
                    xp = (s[0] * xp + s[0] * t[0]);
                    yp = (s[1] * yp + s[1] * t[1]);
                    
                    if (!bOrgin)
                    {
                        ox = (int)x - 300;
                        oy = (int)y - 300;
                        bOrgin = true;
                    }
                    gp.DrawLine(pen, (int)x - ox,
                        Glovar.bmp_vsize - ((int)y - oy), (int)xp - ox,
                        Glovar.bmp_vsize - ((int)yp - oy));
                    pbMap.Image = bmp;
                    pbMap.Update();
                }

                gp.DrawEllipse(penRed, (int)x - ox, Glovar.bmp_vsize - ((int)y - oy),
                    rad, rad);
                pbMap.Image = bmp;
                pbMap.Update();
                Thread.Sleep(0);
                
            }

            trans[0] = -ox / scale[0];
            trans[1] = -oy / scale[1];

            pen.Dispose();
            penRed.Dispose();

        }
        private void pbMap_refresh()
        {
            if (pbMap.Image == null)
                return;
            Bitmap bmp = new Bitmap(Glovar.bmp_hsize, Glovar.bmp_vsize);//pbMap.Image);
            Graphics gp = Graphics.FromImage(bmp);

            int rad = 1;
            Pen pen = new Pen(Color.Blue, 4);
            pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
            pen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
            Pen penRed = new Pen(Color.Red, 4);
            penRed.StartCap = System.Drawing.Drawing2D.LineCap.Round;
            penRed.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
            SolidBrush blush = new SolidBrush(Color.Red);

            double x = 0, y = 0, xp = 0, yp = 0;

            int i;
            Node nd, nd2;
            mForest.StartTravel();
            for (i = 0; i < mForest.Count(); i++)
            {
                nd = mForest.GetNext();
                x = (nd.x * 1) * 10000;
                y = (nd.y * 1) * 10000;
                x = (scale[0] * x + scale[0] * trans[0]);
                y = (scale[1] * y + scale[1] * trans[1]);

                foreach (DictionaryEntry de in nd.edge.edges)
                {

                    Edge ed = (Edge)de.Value;
                    nd2 = mForest.GetNode(ed.id);

                    xp = (nd2.x) * 10000;
                    yp = (nd2.y) * 10000;
                    xp = (scale[0] * xp + scale[0] * trans[0]);
                    yp = (scale[1] * yp + scale[1] * trans[1]);

                    gp.DrawLine(pen, (int)x,
                        Glovar.bmp_vsize - ((int)y), (int)xp,
                        Glovar.bmp_vsize - ((int)yp));
                    
                }

                gp.DrawEllipse(penRed, (int)x, Glovar.bmp_vsize - ((int)y),
                    rad, rad);
            }
            pbMap.Image = bmp;
            pbMap.Update();

            pen.Dispose();
            penRed.Dispose();
        }

        private Nodes mForest=null;
       
        private int mx=0, my=0;
        private double[] scale=null, trans=null;
        private bool isMouseDown=false;
        private int index;

        private void pbMap_MouseDown(object sender, MouseEventArgs e)
        {
            isMouseDown = true;
            mx = e.X;
            my = e.Y;

        }
        private void pbMap_MouseWheel(object sender, MouseEventArgs e)
        {
            if (trans == null || scale == null || mForest == null)
                return;

            double delta = e.Delta /1200.0;
            
            scale[0] += delta;
            scale[1] += delta;
            scale[2] += delta;

            pbMap_refresh();
            
        }

        private void pbMap_MouseMove(object sender, MouseEventArgs e)
        {
            if (trans == null || scale == null || isMouseDown == false || mForest == null)
                return;

            int dx, dy;
            dx = e.X - mx;
            dy = my - e.Y;

            mx = e.X;
            my = e.Y;

            trans[0] += dx;
            trans[1] += dy;
            pbMap_refresh();
        }

        private void pbMap_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }
    }
}
